package com.linkallcloud.web.face.base;

import com.linkallcloud.core.domain.Domain4Company;
import com.linkallcloud.core.dto.CheckResult;
import com.linkallcloud.core.dto.Trace;
import com.linkallcloud.core.exception.BizException;
import com.linkallcloud.core.exception.Exceptions;
import com.linkallcloud.core.face.message.request.FaceRequest;
import com.linkallcloud.core.face.message.request.IdFaceRequest;
import com.linkallcloud.core.face.message.request.ListFaceRequest;
import com.linkallcloud.core.face.message.request.ObjectFaceRequest;
import com.linkallcloud.core.face.message.request.PageFaceRequest;
import com.linkallcloud.core.face.message.request.ParentIdFaceRequest;
import com.linkallcloud.core.face.message.request.StatusFaceRequest;
import com.linkallcloud.core.lang.Strings;
import com.linkallcloud.core.manager.IManager;
import com.linkallcloud.core.query.WebQuery;
import com.linkallcloud.core.www.ISessionBUser;
import com.linkallcloud.core.www.ISimpleUser;

public abstract class CompanyFilterFace<T extends Domain4Company, S extends IManager<T>> extends BaseFace<T, S> {

    @Override
    public CheckResult check(Trace t, String methodName, FaceRequest fr, ISimpleUser su) {
        if ("deletes".equals(methodName) || "changeStatuss".equals(methodName)) {
            throw new BizException(Exceptions.CODE_ERROR_AUTH_PERMISSION, "您无权执行此操作");
        }
        return super.check(t, methodName, fr, su);
    }

    @SuppressWarnings("unchecked")
    public CheckResult saveCheck(Trace t, String methodName, FaceRequest freq, ISimpleUser su) {
        if (freq != null) {
            ObjectFaceRequest<T> fr = (ObjectFaceRequest<T>) freq;
            if (fr.getData() != null) {
                T entity = fr.getData();
                if (entity.getId() == null) {
                    return doSaveCheckAdd(t, entity, su);
                } else {
                    return doSaveCheckUpdate(t, entity, su);
                }
            }
        }
        return null;
    }

    protected CheckResult doSaveCheckUpdate(Trace t, T entity, ISimpleUser suser) {
        ISessionBUser su = (ISessionBUser) suser;
        if (su.getLoginName().equals("superadmin")) {
            return null;
        } else {
            T dbEntity = manager().fetchByIdUuid(t, entity.getId(), entity.getUuid());
            if (dbEntity != null) {
                if (su.companyId().equals(dbEntity.getCompanyId())
                        && su.getUserType().equals(dbEntity.getCompanyType())) {
                    return new CheckResult(dbEntity);
                }
            }
        }
        throw new BizException(Exceptions.CODE_ERROR_AUTH_PERMISSION, "你无权执行此操作！！！");
    }

    protected CheckResult doSaveCheckAdd(Trace t, T entity, ISimpleUser suser) {
        ISessionBUser su = (ISessionBUser) suser;
        entity.setCompanyId(su.companyId());
        entity.setCompanyType(su.getUserType());
        return null;
    }

    public CheckResult deleteCheck(Trace t, String methodName, FaceRequest freq, ISimpleUser suser) {
        IdFaceRequest fr = (IdFaceRequest) freq;
        return checkEntity4CompanyById(t, fr, suser);
    }

    public CheckResult changeStatusCheck(Trace t, String methodName, FaceRequest freq, ISimpleUser suser) {
        StatusFaceRequest fr = (StatusFaceRequest) freq;
        return checkEntity4CompanyById(t, fr, suser);
    }

    public CheckResult fetchCheck(Trace t, String methodName, FaceRequest freq, ISimpleUser suser) {
        ParentIdFaceRequest fr = (ParentIdFaceRequest) freq;
        if (fr.getId() != null && !Strings.isBlank(fr.getUuid())) {
            return checkEntity4CompanyById(t, fr, suser);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected T doFetch(Trace t, ParentIdFaceRequest fr, ISimpleUser suser, CheckResult cr) {
        if (cr != null && cr.getResult() != null) {
            return (T) cr.getResult();
        } else {
            return super.doFetch(t, fr, suser, cr);
        }
    }

    public CheckResult checkEntity4CompanyById(Trace t, IdFaceRequest fr, ISimpleUser suser) {
        ISessionBUser su = (ISessionBUser) suser;
        if (su.getLoginName().equals("superadmin")) {
            return null;
        } else {
            T dbEntity = manager().fetchByIdUuid(t, fr.getId(), fr.getUuid());
            if (dbEntity != null) {
                if (su.companyId().equals(dbEntity.getCompanyId())
                        && su.getUserType().equals(dbEntity.getCompanyType())) {
                    return new CheckResult(dbEntity);
                }
            }
            throw new BizException(Exceptions.CODE_ERROR_AUTH_PERMISSION, "你无权执行此操作！！！");
        }
    }

    public CheckResult pageCheck(Trace t, String methodName, FaceRequest freq, ISimpleUser su) {
        PageFaceRequest fr = (PageFaceRequest) freq;
        if (fr.getQuery() == null) {
            fr.setQuery(new WebQuery());
        }
        filterCompanyWebQuery(fr.getQuery(), su, suVisitAllCompaniesData());
        return null;
    }

    public CheckResult page4SelectCheck(Trace t, String methodName, FaceRequest freq, ISimpleUser su) {
        PageFaceRequest fr = (PageFaceRequest) freq;
        if (fr.getQuery() == null) {
            fr.setQuery(new WebQuery());
        }
        filterCompanyWebQuery(fr.getQuery(), su, suVisitAllCompaniesData());
        return null;
    }

    public CheckResult findCheck(Trace t, String methodName, FaceRequest freq, ISimpleUser su) {
        ListFaceRequest fr = (ListFaceRequest) freq;
        if (fr.getQuery() == null) {
            fr.setQuery(new WebQuery());
        }
        filterCompanyWebQuery(fr.getQuery(), su, suVisitAllCompaniesData());
        return null;
    }

    /**
     * 超级管理员是否需要看所有单位的数据
     * 
     * @return
     */
    protected boolean suVisitAllCompaniesData() {
        return false;
    }

}
